<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8" style="margin-top: 20px">
      <el-card shadow="hover">
        <div class="user">
          <img :src="getImageUrl('user')" class="user" />
          <div class="user-info">
            <p>Admin</p>
            <p>超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间:<span>2025-8-11</span></p>
          <p>上次登录的地点:<span>中国朝鲜省</span></p>
        </div>
      </el-card>

      <el-card shadow="hover" class="table">
        <el-table :data="tableData">
          <el-table-column
            v-for="(val, key) in tableLabel"
            :key="key"
            :prop="key"
            :label="val"
          >
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>

    <el-col :span="16" style="margin-top: 20px">
      <div class="num">
        <el-card
          :body-style="{ display: 'flex', padding: 0 }"
          v-for="item in countData"
          :key="item.name"
        >
          <component
            :is="item.icon"
            class="icons"
            :style="{ color: item.color }"
          ></component>

          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="txt">￥{{ item.name }}</p>
          </div>
        </el-card>
      </div>

      <el-card class="top-echart">
        <div ref="echartRef" style="height: 240px;"></div>
      </el-card>
      <div class="graph">
        <el-card>
          <div ref="userEchart" style="height: 240px;"></div>
        </el-card>
        <el-card>
          <div ref="videoEchart" style="height: 240px;"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, onMounted } from 'vue'
import * as echarts from 'echarts'
const { proxy } = getCurrentInstance()

import getTableDataApi from '../api/api'
import { getCountData, getChartData } from '../api/api'

const getImageUrl = user => {
  return new URL(`../assets/images/${user}.png`, import.meta.url).href
}
const tableData = ref([])
const countData = ref([])

const tableLabel = ref({
  name: '品牌',
  todayBuy: '今日购买',
  monthBuy: '本月购买',
  totalBuy: '总购买'
})

const getTableDataApiFn = async () => {
  let data = await getTableDataApi()

  tableData.value = data.tableData
}
const getCountDataApiFn = async () => {
  let data = await getCountData()
  countData.value = data
}

//observer 接收观察器实例对象
const observer = ref(null)
const echartRef = ref(null)

//这个是折线图和柱状图 两个图表共用的公共配置
const xOptions = reactive({
  // 图例文字颜色
  textStyle: {
    color: '#333'
  },
  legend: {},
  grid: {
    left: '20%'
  },
  // 提示框
  tooltip: {
    trigger: 'axis'
  },
  xAxis: {
    type: 'category', // 类目轴
    data: [],
    axisLine: {
      lineStyle: {
        color: '#17b3a3'
      }
    },
    axisLabel: {
      interval: 0,
      color: '#333'
    }
  },
  yAxis: [
    {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#17b3a3'
        }
      }
    }
  ],
  color: ['#2ec7c9', '#b6a2de', '#5ab1ef', '#ffb980', '#d87a80', '#8d98b3'],
  series: []
})

const pieOptions = reactive({
  tooltip: {
    trigger: 'item'
  },
  legend: {},
  color: [
    '#0f78f4',
    '#dd536b',
    '#9462e5',
    '#a6a6a6',
    '#e1bb22',
    '#39c362',
    '#3ed1cf'
  ],
  series: []
})

//请求图表数据并渲染的方法
const getChartDataFn = async () => {
  const { orderData, userData, videoData } = await getChartData()
  //对第一个图表的xAxis和series赋值
  console.log('orderData',orderData);
  xOptions.xAxis.data = orderData.date
  xOptions.series = Object.keys(orderData.data[0]).map(val => {
    return {
      name: val,
      data: orderData.data.map(item => item[val]),
      type: 'line'
    }
  })

  const OneEcharts = echarts.init(proxy.$refs['echartRef'])
  // const OneEcharts = echarts.init(echartRef.value)

  OneEcharts.setOption(xOptions)

  //对第二个图表的xAxis和series赋值
  xOptions.xAxis.data = userData.map(item => item.date)
  xOptions.series = [
    {
      name: '新增用户',
      data: userData.map(item => item.new),
      type: 'bar'
    },
    {
      name: '活跃用户',
      data: userData.map(item => item.active),
      type: 'bar'
    }
  ]
  //two
  const TwoEcharts = echarts.init(proxy.$refs['userEchart'])
  TwoEcharts.setOption(xOptions)

  // //对第三个图表的series赋值
  pieOptions.series = [
    {
      data: videoData,
      type: 'pie'
    }
  ]
  // //three
  const ThreeEcharts = echarts.init(proxy.$refs['videoEchart'])
  ThreeEcharts.setOption(pieOptions)

  //ResizeObserver 如果监视的容器大小变化，如果改变会执行传递的回调
  observer.value = new ResizeObserver(entries => {
    OneEcharts.resize()
    TwoEcharts.resize()
    ThreeEcharts.resize()
  })
  // //如果这个容器存在
  if (proxy.$refs['echartRef']) {
    //则调用监视器的observe方法，监视这个容器的大小
    observer.value.observe(proxy.$refs['echartRef'])
  }
}

onMounted(() => {
  getTableDataApiFn()

  getCountDataApiFn()
  getChartDataFn()
})
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  overflow: hidden;
  .user {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;
    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
    }
  }
  .login-info {
    p {
      line-height: 30px;
      font-size: 14px;
      color: #999;
      span {
        color: #666;
        margin-left: 60px;
      }
    }
  }
  .table {
    margin-top: 20px;
  }
  .num {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .el-card {
      width: 32%;
      margin-bottom: 20px;
    }
    .icons {
      width: 80px;
      height: 80px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
    }
    .detail {
      margin-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .num {
        font-size: 30px;
        margin-bottom: 10px;
      }
      .txt {
        font-size: 14px;
        text-align: center;
        color: #999;
      }
    }
  }
  .top-echart {
    height: 280px;
    // width: 100%;
  }
  .graph {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    .el-card {
      width: 48%;
      height: 260px;
    }
  }
}
</style>
